package com.nibian.nibian.utils;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by zhangguobao on 2017/6/26.
 */

public class DialogUtils {
    public static Dialog showCustomDialog(Context context, View contentView, int dialogTheme, int dialogAnim, float widthPer, int gravityPosition) {
        Dialog dialog = new Dialog(context, dialogTheme);
        // 设置显示View
        if (null != contentView) {
            dialog.setContentView(contentView);
        }
        // 设置显示位置
        dialog.getWindow().setGravity(gravityPosition);
        // 设置是否触摸对话框以外隐藏对话框
        dialog.setCanceledOnTouchOutside(true);
        // 设置是否允许按返回键隐藏对话框
        dialog.setCancelable(true);
        // 设置对话框大小
        DisplayMetrics displayMetrics = new DisplayMetrics();
        dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        if (dialogAnim != 0)
            dialog.getWindow().setWindowAnimations(dialogAnim);
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();

        params.width = (int) (displayMetrics.widthPixels * widthPer);
        return dialog;
    }
}
